package com.example.mongo_demo.service.impl;

import com.example.mongo_demo.entity.Student;
import com.example.mongo_demo.service.StudentService;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Student save(Student student) {
        return mongoTemplate.save(student);
    }

    @Override
    public long remove(Student student) {
        DeleteResult result = mongoTemplate.remove(student);
        return result.getDeletedCount();
    }

    @Override
    public long remove(Query query) {
        DeleteResult result = mongoTemplate.remove(query,Student.class);
        return result.getDeletedCount();
    }

    @Override
    public Student findById(Serializable id) {
        return mongoTemplate.findById(id,Student.class);
    }
}
